package cn.bdqn.t354.controller;

import cn.bdqn.t354.pojo.Customer;
import cn.bdqn.t354.pojo.Dict;
import cn.bdqn.t354.service.CustomerService;
import cn.bdqn.t354.service.DictService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Resource
    private CustomerService customerService;
    @Resource
    DictService dictService;

    /**
     * 列表展示
     *
     * @param custName
     * @param custNo
     * @param region
     * @param managerName
     * @param level
     * @param pageIndex
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(String custName, String custNo, String region,
                       String managerName, String level,
                       @RequestParam(defaultValue = "1") Integer pageIndex,
                       Model model) {
        //设置每页显示条数
        int pageSize = 3;

        //查询每页显示的数据
        IPage<Customer> customerPager =
                customerService.getCustomersByPage(custName, custNo, region, managerName, level, pageIndex, pageSize);
        model.addAttribute("customerPager", customerPager);

        //查询条件回显
        model.addAttribute("custName", custName);
        model.addAttribute("custNo", custNo);
        model.addAttribute("region", region);
        model.addAttribute("managerName", managerName);
        model.addAttribute("level", level);

        //查询客户等级
        List<Dict> levelList = dictService.getDictByDictType("客户等级");
        model.addAttribute("levelList", levelList);

        //查询地区
        List<Dict> regionList = dictService.getDictByDictType("地区");
        model.addAttribute("regionList", regionList);

        return "customer/list";
    }

    /**
     * 跳转到修改页面
     *
     * @param custNo
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(String custNo, Model model) {
        //根据客户编号，查询客户信息
        Customer customer = customerService.getCustomer(custNo);
        model.addAttribute("customer", customer);

        //查询客户等级
        List<Dict> levelList = dictService.getDictByDictType("客户等级");
        model.addAttribute("levelList", levelList);

        //查询地区
        List<Dict> regionList = dictService.getDictByDictType("地区");
        model.addAttribute("regionList", regionList);

        return "customer/edit";
    }

    /**
     * 修改操作
     *
     * @param customer
     * @return
     */
    @RequestMapping("/update")
    public String update(Customer customer) {
        try {
            customerService.updateCustomer(customer);
            //跳转到列表页面
            return "redirect:/customer/list";
        } catch (Exception e) {
            e.printStackTrace();
            //跳转到编辑页面
            return "redirect:/customer/edit";
        }
    }

    /**
     * 删除操作
     *
     * @param custNo
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(String custNo) {
        Map<String, Object> map = new HashMap<>();
        try {
            customerService.deleteCustomer(custNo);
            map.put("delResult", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult", false);
        }
        return map;
    }

}
